package com.glela.api.upload.qiniu.impl;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.httpclient.HttpException;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

/**
 * 
 */
public class HttpGet {
    private HttpGet(){};

    public static void httpGet(String url) throws HttpException, Exception {
    	
//    	HttpRequest request = new HttpRequest(HttpResultType.STRING);
//		//设置格式码
//		request.setCharset("utf-8");
//		request.setHead("Content-Type", "application/x-www-form-urlecnoded");
//		//设置超时时间
//		request.setConnectionTimeout(3000);
//		//get类型提交
//		request.setMethod("GET");
//		//设置请求的参数ID
////		request.setQueryString("id=" + id);
//		//查询七牛的URL
//		request.setUrl(url);
//		HttpResponse response = HttpProtocolHandler.getInstance().execute(request);
//		Header[] headers = new Header[1];
//		headers[0] = new Header();
//		headers[0].setName("Content-Type");
//		headers[0].setValue("application/json");
//		response.setResponseHeaders(headers);
//		byte[] res = response.getByteResult();
		 
//		String str = new String(res);
// 		 System.out.println(str);
         //OkHttpClient client = new OkHttpClient();

       /* Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/x-www-form-urlecnoded")
                .build();
        try {
            Response response = client.newCall(request).execute();
            System.out.println("StatusCode is " + response.code());
            System.out.println(response.headers().toString());
            //response.body().bytes();
            System.out.println(new String(response.body().bytes()));
        } catch (IOException e) {
            System.out.println(e.toString());
        }*/
    	OkHttpClient client = new OkHttpClient();

    	 Request request = new Request.Builder()
         .url(url)
         .addHeader("Content-Type", "application/x-www-form-urlecnoded")
         .build();
    	 try {
             Response response = client.newCall(request).execute();
             System.out.println("StatusCode is " + response.code());
             System.out.println(response.headers().toString());
             System.out.println(new String(response.body().bytes()));
//             Object  avinfo=new String(response.body().bytes());
             
//             System.out.println(avinfo);
//             Object object=SerializeUtil.deserializeToObject(response.body().bytes());
//             String  str=JSONObject.toJSONString(object);
//             System.out.println(str);
             
//             	StringMap sm=Json.decode(new String(response.body().bytes()));
//             	
//             	sm.get("");
             FileInputStream is = new FileInputStream("D:/TestFile.txt");   
             // 设定读取的字节数   
             int n = 512;   
             byte buffer[] = response.body().bytes();   
             // 读取输入流   
             while ((is.read(buffer, 0, n) != -1) && (n > 0)) {   
                 System.out.print(new String(buffer));   
             }   
             System.out.println();   
             // 关闭输入流   
             is.close();   
          
             
             
         } catch (IOException e) {
        	 e.printStackTrace();
             System.out.println(e.toString());
         }


    	
    	
    }

}
